﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Practices.Prism.Interactivity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;

namespace AesonXamlControls.Interaction.AesonPopup
{
    public class AesonPopupAction : PopupWindowAction
    {
        protected override void PrepareContentForWindow(INotification notification, Window wrapperWindow)
        {

            AesonChildWindows p = wrapperWindow as AesonChildWindows;
            p.MyContent.Content = this.WindowContent;
        }
        protected override void Invoke(object parameter)
        {
            var args = parameter as InteractionRequestedEventArgs;
            AesonChildWindows wrapperWindow = this.GetWindow(args.Context) as AesonChildWindows;



            var callback = args.Callback;
            EventHandler handler = null;
            handler =
                (o, e) =>
                {
                    wrapperWindow.Closed -= handler;
                    wrapperWindow.Content = null;
                    callback();
                };
            wrapperWindow.Closed += handler;

            if (this.CenterOverAssociatedObject)
            {
                SizeChangedEventHandler sizeHandler = null;
                sizeHandler =
                    (o, e) =>
                    {
                        wrapperWindow.SizeChanged -= sizeHandler;

                        FrameworkElement invoker = this.AssociatedObject;
                        Point position = invoker.PointToScreen(new Point(0, 0));

                        wrapperWindow.Top = position.Y + ((invoker.ActualHeight - wrapperWindow.ActualHeight) / 2);
                        wrapperWindow.Left = position.X + ((invoker.ActualWidth - wrapperWindow.ActualWidth) / 2);
                    };
                wrapperWindow.SizeChanged += sizeHandler;
            }


            if (this.IsModal)
            {
                wrapperWindow.ShowDialog();
            }
            else
            {
                wrapperWindow.Show();
            }
        }
        protected override Window GetWindow(INotification notification)
        {
            return GetCustomWindow(notification);
        }
        protected AesonChildWindows GetCustomWindow(INotification notification)
        {
            AesonChildWindows wrapperWindow;
            if (this.WindowContent != null)
            {
                wrapperWindow = this.MyCreateDefaultWindow(notification);

                wrapperWindow.MyContent.DataContext = (object)notification;
                wrapperWindow.Title = notification.Title;

                this.PrepareContentForWindow(notification, wrapperWindow);

            }
            else
            {
                wrapperWindow = this.MyCreateDefaultWindow(notification);
            }

            return wrapperWindow;
        }
        private AesonChildWindows MyCreateDefaultWindow(INotification notification)
        {
            AesonChildWindows window;

            window = new AesonChildWindows()
            {
                Confirmation = (IConfirmation)notification,


            };

            window.MyContent.Content = WindowContent;
            return window;
        }

    }
}
